package com.yifushidai.api;

import com.yifushidai.service.PowerService;
import com.yifushidai.utils.Query;
import com.yifushidai.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@Api(tags = "控电记录")
@RequestMapping("/api/power")
public class PowerController {

    @Autowired
    private PowerService powerService;

    @PostMapping("/querySaveDetails")
    @ApiOperation(value = "省电详情接口", notes = "省电详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public R details(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                     @ApiParam(value = "查询类型 1：周 2：月 3：季度 4：年", required = true) @RequestParam int qtype,
                     @ApiParam(value = "起始页", required = true) @RequestParam int pageIndex,
                     @ApiParam(value = "页大小", required = true) @RequestParam int pageSize) throws Exception {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("page", pageIndex);
        params.put("limit", pageSize);
        params.put("offset", (pageIndex - 1) * pageSize);
        params.put("mac", mac);
        Query query = new Query(params);
        return powerService.details(qtype, query);
    }

    @PostMapping("/control")
    @ApiOperation(value = "控电记录接口", notes = "控电记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public R control(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                     @ApiParam(value = "查询类型 0：当天 1：周 2：月 3：季度 4：年", required = true) @RequestParam int qtype,
                     @ApiParam(value = "起始页", required = true) @RequestParam int pageIndex,
                     @ApiParam(value = "页大小", required = true) @RequestParam int pageSize) throws Exception {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("page", pageIndex);
        params.put("limit", pageSize);
        params.put("offset", (pageIndex - 1) * pageSize);
        params.put("mac", mac);
        Query query = new Query(params);
        return powerService.control(qtype, query);
    }

    @ApiOperation("同步刷新节电数据")
    @PostMapping("sync")
    public R sync() throws Exception {
        return powerService.sync();
    }
}
